package me.pinion.controller;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.ActionKey;
import me.pinion.Log;
import me.pinion.check.annotation.AllowMethod;
import me.pinion.check.annotation.AllowRole;
import me.pinion.controller.annotation.RouteBind;
import me.pinion.model.User;
import me.pinion.model.UserInfo;
import me.pinion.service.AccountService;

import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;

@RouteBind("/account")
public class AccountController extends Controller {

    AccountService accountService;



    @AllowRole(["USER"])
    public void center(){
        String page = "center";
        if (getPara(0) != null){
            page = getPara(0);
        }
        render("/freemarker/account/" + page + ".ftl");
    }

    @AllowMethod(["POST"])
    public void set_avatar(){
        String url = getPara("url");
        String pageCover = getPara("page_cover");
        User user = this.curUser();
        if (url != null){
            user.set("avatar", url);
        }
        if (pageCover != null){
            UserInfo userInfo = user.getUserInfo();
            userInfo.set("page_cover", pageCover);
            userInfo.update();
        }
        user.update();
        renderJson(user);
    }

    @AllowMethod(["POST"])
    @AllowRole(["USER"])
    public void set_baseinfo(){
        User user = this.curUser();
        UserInfo userInfo = user.getUserInfo();
        Enumeration en = getParaNames();
        while (en.hasMoreElements()){
            String name = (String) en.nextElement();
            userInfo.set(name, getPara(name));
        }
        userInfo.set("gender", getParaToBoolean("gender"));
        userInfo.update();
        renderJson(user.getUserInfo());
    }

    @AllowMethod(["POST"])
    @AllowRole(["USER"])
    public void set_password(){
        User currentUser = this.curUser();
        if (accountService.login(this, currentUser.getStr("username"), getPara("password"))){
            currentUser.setPassword(getPara("newpassword"));
            currentUser.update();
            renderJson(currentUser);
        }else {
            renderError(HttpServletResponse.SC_BAD_REQUEST);
        }
    }


    @AllowMethod()
    public void notify_message(){
        renderFreeMarker("/freemarker/message/notify_message_list.ftl");
    }




    @ActionKey("/register")
    public void register(){
        if(getRequest().getMethod().equals("POST")){
            try {
                User user = accountService.register(this);
                redirect("/");
            }catch (Exception e){
                Log.e(e);
                render("/account/register.ftl");
            }
        }else {
            render("/account/register.ftl");
        }
    }

    @ActionKey("/login")
    public void login(){
        if (getRequest().getMethod().equals("POST")){
            String account = getPara("account");
            String password = getPara("password");
            if (accountService.login(this, account, password)){
                this.curUser().getUserInfo();

                if (getPara("template") != null){
                    render(getPara("template"));
                }else {
                    redirect("/book");
                }
                return;
            }else {
                renderError(HttpServletResponse.SC_BAD_REQUEST);
            }
        }
        render("/account/login.ftl");
    }

    @ActionKey("/logout")
    public void logout(){
        this.setSessionAttr("user", new User());
        redirect("/");
    }

    public void exist(){
        JSONObject json = new JSONObject();
        json.put("exist", accountService.exist(this));
        renderJson(json.toJSONString());
    }
}
